package t1011;


import java.util.Scanner;

/**
 * @姓名 张东升
 * @日期 2025年04月2025/4/9日、
 * 23 2 42 4 1121 27 40 0
 */
public class Main {
    public static void main(String[] args) {
        // 创建连表
        Node head = new Node();
        Scanner scanner = new Scanner(System.in);
        int length = createLinkList(head, scanner);
        // 输出链表
        display(head, length);
        // 符合格式
        display(new Node(), 0);
        // 拆分奇数
        Node odd = new Node();
        length = splitOddNumbers(head, odd);
        reversal(odd);
        display(odd, length);
        // 拆分偶数
        Node even = new Node();
        length = splitEvenNumbers(head, even);
        // 输出偶数链表
        reversal(even);
        display(even, length);
    }

    public static int createLinkList(Node head, Scanner scanner) {
        int count = 0;
        Node temp = head;
        int n = scanner.nextInt();
        while (n != 0) {
            temp.next = new Node(n, null);
            temp = temp.next;
            n = scanner.nextInt();
            count++;
        }
        return count;
    }

    public static void display(Node head, int count) {
        Node next = head.next;
        System.out.println("The length:" + count);
        System.out.print("The elements:");
        while (next != null) {
            System.out.print(next.data + " ");
            next = next.next;
        }
        System.out.println();
    }


    public static int splitEvenNumbers(Node head, Node even) {
        int count = 0;
        Node node = head;
        Node current = head.next;
        while (current != null) {
            if (current.data % 2 == 0) {
                even.next = current;
                even = even.next;
                node.next = current.next; // 从原始链表中删除当前节点
                count++;
            } else {
                node = current;
            }
            current = current.next;
        }
        even.next = null; // 确保偶数链表的末尾指向null
        return count;
    }

    public static int splitOddNumbers(Node head, Node odd) {
        int count = 0;
        Node node = head;
        Node current = head.next;
        while (current != null) {
            if (current.data % 2 != 0) {
                odd.next = current;
                odd = odd.next;
                node.next = current.next; // 从原始链表中删除当前节点
                count++;
            } else {
                node = current;
            }
            current = current.next;
        }
        odd.next = null; // 确保偶数链表的末尾指向null
        return count;
    }

    public static void reversal(Node head) {
        Node currNode = head.next;
        Node temp = null;
        Node node = null;
        while (currNode != null) {
            temp = currNode.next;
            currNode.next = node;
            node = currNode;
            currNode = temp;
        }
        head.next = node;
    }
}


class Node {
    public int data;
    public Node next;

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Node() {
    }
}
